package siau.android.base

import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import hhm.android.library.utils.GlideUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
import siau.android.http.model.AdvertisementModel
import siau.android.library.KeyString

class AdActivityDialog : AppCompatActivity(), CoroutineScope by MainScope() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_advertisement)

        val data = intent.getSerializableExtra(KeyString.data) as AdvertisementModel

        val iv = findViewById<ImageView>(R.id.dialog_advertisement_iv)
        GlideUtils.loadImage(this, data.imageUrl, iv, R.mipmap.no_data, R.mipmap.no_data)
        val sure = findViewById<TextView>(R.id.dialog_advertisement_sure)
        val cancel = findViewById<TextView>(R.id.dialog_advertisement_cancel)
        val noTip = findViewById<LinearLayout>(R.id.dialog_advertisement_no_tip)
//        if (data.canHide == 1) {
//            setDialogWindowAttr(dialog, context, 270f, 408f)
//            noTip.visibility = View.VISIBLE
//        } else {
//            setDialogWindowAttr(dialog, context, 270f, 376f)
        noTip.visibility = View.GONE
//        }
        val cb = findViewById<CheckBox>(R.id.dialog_advertisement_cb)
        sure.setOnClickListener {
            if (data.skipType == 1) {
                //跳转APP
                if (data.skipPath.startsWith("taobao")) {
                    if (SystemUtil.isPkgInstalled(this, "com.taobao.taobao")) {
                        SystemUtil.gotoShop(this, data.skipPath);
                    } else {
                        Toast.makeText(this, "您还没有安装淘宝客户端!", Toast.LENGTH_SHORT).show();
                    }
                } else if (data.skipPath.startsWith("openapp.jdmobile")) {
                    if (SystemUtil.isPkgInstalled(this, "com.jingdong.app.mall")) {
                        SystemUtil.gotoShop(this, data.skipPath);
                    } else {
                        Toast.makeText(this, "您还没有安装京东客户端!", Toast.LENGTH_SHORT).show();
                    }
                }
            } else if (data.skipType == 2) {
                //跳转外部浏览器
            } else {
                //跳转内部浏览器
            }
            finish()
        }
        cancel.setOnClickListener {
            finish()
        }
    }
}